import 'package:flutter/material.dart';
import 'package:flutter_oldman_health/utils/remote_control_utils.dart';

class ListenerContainer extends StatelessWidget {
  //手指按下的位置
  double _downY, _downX = 0.0;

  Widget child;
  Size size;
  final void Function() sendTouchScreenMessage;
  ScreenPoint pointStart = ScreenPoint(0, 0);

//void Function() callback
  ListenerContainer({this.size, this.child, this.sendTouchScreenMessage});

  int index = 0;

  @override
  Widget build(BuildContext context) {
    return Listener(
        onPointerDown: (PointerDownEvent event) {
          touchScreenMessage(0, event);
        },
        onPointerMove: (PointerMoveEvent event) {
          touchScreenMessage(2, event);
        },
        onPointerCancel: (PointerCancelEvent event) {
          touchScreenMessage(3, event);
        },
        onPointerUp: (PointerUpEvent event) {
          touchScreenMessage(1, event);
        },
        child: AnimatedSwitcher(
          child: child,
          duration: Duration(milliseconds: 300),
          switchInCurve: Curves.easeInOutBack,
          transitionBuilder: (child, animation) =>
              ScaleTransition(scale: animation, child: child),
        ));
  }

  void touchScreenMessage(int type, PointerEvent event) {
    index++;

    _downY = event.position.dy;
    _downX = event.position.dx;

    pointStart.x = 100 * _downX ~/ size.width;
    pointStart.y = 100 * _downY ~/ size.height;
    RemoteControlTool().sendTouchScreenMessage(type, index, pointStart);
  }
}

class ScreenPoint {
  int x, y;

  ScreenPoint(this.x, this.y);

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['x'] = this.x;
    data['y'] = this.y;
    return data;
  }
}
